package com.chapter15.generic_;

import java.util.ArrayList;
import java.util.Comparator;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
public class GenericExercise01 {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("jack", 2000, new MyDate(2022, 1, 1)));
        employees.add(new Employee("dav", 5000, new MyDate(2020, 1, 1)));
        employees.add(new Employee("jack", 3000, new MyDate(2021, 1, 1)));

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                int i = o1.getName().compareTo(o2.getName());
                if (i == 0) {
                    i = o1.getMyDate().getYear() - o2.getMyDate().getYear();
                }
                if (i == 0) {
                    i = o1.getMyDate().getMonth() - o2.getMyDate().getMonth();
                }
                if (i == 0) {
                    i = o1.getMyDate().getDay() - o2.getMyDate().getDay();
                }
//                i = i == 0 ? o1.getMyDate().getYear() - o2.getMyDate().getYear() : 1;
//                i = i == 0 ? o1.getMyDate().getMonth() - o2.getMyDate().getMonth() : 1;
//                i = i == 0 ? o1.getMyDate().getDay() - o2.getMyDate().getDay() : 1;
                return i;
            }
        });
        for (Object o : employees) {
            System.out.println(o);
        }


    }
}

class Employee {
    private String name;
    private double sal;
    private MyDate myDate;

    public Employee(String name, double sal, MyDate myDate) {
        this.name = name;
        this.sal = sal;
        this.myDate = myDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public MyDate getMyDate() {
        return myDate;
    }

    public void setMyDate(MyDate myDate) {
        this.myDate = myDate;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", myDate=" + myDate +
                '}';
    }
}

class MyDate {
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }
}
